﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;

namespace DomainName
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetJSON();

        }
        /// <summary>
        /// 公网IP接口：http://www.net.cn/static/customercare/yourIP.asp
        /// IP查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=
        /// </summary>
        private void GetJSON()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.net.cn/static/customercare/yourIP.asp");
            WebResponse response = request.GetResponse();
            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                Regex regex = new Regex(@"\d\d{1,2}.\d{1,3}.\d{1,3}.\d{1,3}");
                string urlResponse = sr.ReadToEnd();
                string outIP = regex.Match(urlResponse).Value;
                request.Abort();
                string detailInformatinoRequest = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + outIP;
                HttpWebRequest detailRequest = (HttpWebRequest)WebRequest.Create(detailInformatinoRequest);
                WebResponse detailResponse = detailRequest.GetResponse();
                using (StreamReader srDetail = new StreamReader(detailResponse.GetResponseStream(), Encoding.UTF8))
                {
                    string detailInformationJSON = srDetail.ReadToEnd();
                    IPModel model = Json.GetJSONDeserialize<IPModel>(detailInformationJSON);
                    //string temp = detailInformationJSON.Replace(@"\\", @"\");
                    //string detailInformationDeal = System.Net.WebUtility.HtmlDecode(detailInformationJSON.Replace(@"\\",@"\"));
                    //string hasDealed=  server.HtmlDecode(detailInformationJSON);
                    MessageBox.Show(model.GetIPDetail());
                }
                
            }
        }
    }
}
